//
//  RBProcessOrderListCell.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/16.
//

#import "RBProcessOrderListCell.h"
#import "RBGradientView.h"
#import "RBProcessOrderDetailVC.h"
#import "RBProcessOrderShopAddressVC.h"

@interface RBProcessOrderListCell()
//顶部颜色背景
@property (nonatomic, strong) UIView *topLineView;
//店铺头像
@property (nonatomic, strong) UIImageView *shopImgView;
//店铺名称
@property (nonatomic, strong) UILabel *shopNameL;
//商品状态
@property (nonatomic, strong) UILabel *stateL;
//患者姓名
@property (nonatomic, strong) UILabel *pacientL;
//患者性别
@property (nonatomic, strong) UILabel *genderL;
//商品图片
@property (nonatomic, strong) UIImageView *goodsImgView;
//商品名称
@property (nonatomic, strong) UILabel *goodsNameL;
//按钮背景
@property (nonatomic, strong) UIView *actionView;

@property (nonatomic, strong) RBCellModel *cellModel;

@end

@implementation RBProcessOrderListCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIView *view = UIView.new;
        [view rbSetCornerRadius:Zoom(5)];
        [self.contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(Zoom(345));
            make.edges.mas_equalTo(0);
        }];
        
        RBGradientView *bgView = [RBGradientView gradientViewWithColors:@[[UIColor colorWithHexString:@"#D9E6FF"],UIColor.whiteColor] locations:@[@0, @0.3] vertical:YES];
        [view addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        self.topLineView = UIView.new;
        [view view_addView:self.topLineView withHeight:Zoom(7) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            
            [view h_view_addView:({
                UIImageView *imgV = [RanTool imgViewWithMode:2 radius:Zoom(6) imgName:@""];
                self.shopImgView = imgV;
                imgV;
            }) withHeight:Zoom(36) width:Zoom(36) top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:0];
            
            [view h_view_addView:({
                UILabel *shopNameL = [RanTool labelWithFontSize:Zoom(15) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@""];
                self.shopNameL = shopNameL;
                shopNameL;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(13) right:CGFLOAT_MIN centerY:YES];

            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:1 color:@"#3974EC" align:0 text:@""];
                [view addPriority:1];
                self.stateL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(13) right:0 centerY:YES];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(15) bottom:0 left:Zoom(12) right:Zoom(12) centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textLightBlackColor] align:0 text:@"患者姓名"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@""];
                self.pacientL = view;
                [view addPriority:1];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(30) bottom:0 left:Zoom(13) right:Zoom(12) centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textLightBlackColor] align:0 text:@"患者性别"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@""];
                [view addPriority:1];
                self.genderL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(15) bottom:0 left:Zoom(13) right:Zoom(12) centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:({
                UIImageView *imgV = [RanTool imgViewWithMode:2 radius:0 imgName:@""];
                self.goodsImgView = imgV;
                imgV;
            }) withHeight:Zoom(71) width:Zoom(71) top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:0];
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@""];
                self.goodsNameL = view;
                view.numberOfLines = 2;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(7) bottom:CGFLOAT_MIN left:Zoom(13) right:0 centerY:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(24) bottom:0 left:Zoom(13) right:Zoom(12) centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            self.actionView = view;
            view;
        }) withHeight:Zoom(30) width:CGFLOAT_MIN top:Zoom(14) bottom:Zoom(13) left:Zoom(7) right:Zoom(7) centerX:0];
    }
    return self;
}

- (void)addActions:(NSArray *)actions
{
    [self.actionView h_view_clear];
    [self.actionView h_view_addView:UILabel.new withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
    for (NSNumber *number in actions)
    {
        [self.actionView h_view_addView:[self actionLWithAction:number.integerValue] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(10) right:0 centerY:0];
    }
}

#pragma mark - Event
//去支付
- (void)clickGoPayAction {
    RBOrderListModel *model = self.cellModel.cellData;
    //
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(1, 0, model);
    }
}
//取消订单
- (void)clickCancelOrderAction {
    RBOrderListModel *model = self.cellModel.cellData;
    //
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(2, 0, model);
    }
}
//商家地址
- (void)checkShopDetailBtnTapped {
    RBOrderListModel *model = self.cellModel.cellData;
    RBProcessOrderShopAddressVC *vc = RBProcessOrderShopAddressVC.new;
    vc.store_id = model.store_id;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}
//查看详情
- (void)checkDetailBtnTapped {
    RBOrderListModel *model = self.cellModel.cellData;
    RBProcessOrderDetailVC *vc = RBProcessOrderDetailVC.new;
    vc.order_id = model.order_id;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}
//确认订单
- (void)confirmReceiveBtnTapped {
    RBOrderListModel *model = self.cellModel.cellData;
    //
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(3, 0, model);
    }
}
//去评价
- (void)appraiseBtnTapped {
    RBOrderListModel *model = self.cellModel.cellData;
    //
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(4, 0, model);
    }
}

#pragma mark - Setter

- (void)setCellModel:(RBCellModel *)cellModel {
    _cellModel = cellModel;
    RBOrderListModel *model = cellModel.cellData;
    //店铺
    [self.shopImgView setImageWithUrl:model.logo placeHolder:nil];
    //店铺名称
    self.shopNameL.text = model.store_name;
    //患者姓名
    self.pacientL.text = model.patient_name;
    //患者性别
    self.genderL.text = model.patient_sex;
    //商品图片
    [self.goodsImgView setImageWithUrl:model.file placeHolder:nil];
    //商品名称
    self.goodsNameL.text = model.goods_name;
    //
    NSString *statusString = @"";
    NSString *topColorString = @"";
    NSArray *actions;
    //订单状态 0未支付 1已支付待发货 2已发货待确认 3确认收货 4已评价 5退款 6已完成 7已取消
    switch ([model.order_status intValue]) {
        case 0 : {
            statusString = @"待付款";
            topColorString = @"#3974EC";
            actions = @[
                @(RBProcessOrderActionCancel),
                @(RBProcessOrderActionCheckDetail),
                @(RBProcessOrderActionPay)
            ];
        }   break;
        case 1 : {
            statusString = @"待发货";
            topColorString = @"#F2AE2C";
            actions = @[
                @(RBProcessOrderActionCheckShopAddress),
                @(RBProcessOrderActionCheckDetail)
            ];
        }   break;
        case 2 : {
            statusString = @"待收货";
            topColorString = @"#3974EC";
            actions = @[
                @(RBProcessOrderActionCheckShopAddress),
                @(RBProcessOrderActionCheckDetail),
                @(RBProcessOrderActionConfirmReceive)
            ];
        }   break;
        case 3 : {
            statusString = @"待评价";
            topColorString = @"#30BC14";
            actions = @[
                @(RBProcessOrderActionCheckShopAddress),
                @(RBProcessOrderActionCheckDetail),
                @(RBProcessOrderActionAppraise)
            ];
        }   break;
        case 4 : {
            statusString = @"已评价";
            topColorString = @"#30BC14";
            actions = @[
                @(RBProcessOrderActionCheckShopAddress),
                @(RBProcessOrderActionCheckDetail)
            ];
        }   break;
        case 5 : {
            statusString = @"退款";
            topColorString = @"#F2AE2C";
            actions = @[
                @(RBProcessOrderActionCheckDetail)
            ];
        }   break;
        case 6 : {
            statusString = @"已完成";
            topColorString = @"#30BC14";
            actions = @[
                @(RBProcessOrderActionCheckShopAddress),
                @(RBProcessOrderActionCheckDetail)
            ];
        }   break;
        case 7 : {
            statusString = @"已取消";
            topColorString = @"#F2AE2C";
            actions = @[
                @(RBProcessOrderActionCheckDetail)
            ];
        }   break;
    }
    self.stateL.text = statusString;
    self.topLineView.backgroundColor = [UIColor colorWithHexString:topColorString];
    
    [self addActions:actions];
}

#pragma mark - Getter

- (UILabel *)actionLWithAction: (RBProcessOrderAction)action
{
    UIColor *color;
    UIColor *bgColor;
    NSString *text;
    UIColor *borderColor;
    SEL sel;
    switch (action) {
        case RBProcessOrderActionPay : {
            color = UIColor.whiteColor;
            bgColor = [RBThemeConfig buttonBlueColor];
            text = @"去支付";
            sel = @selector(clickGoPayAction);
        }   break;
        case RBProcessOrderActionCancel : {
            color = [RBThemeConfig textGrayColor];
            bgColor = UIColor.whiteColor;
            text = @"取消订单";
            borderColor = [RBThemeConfig textGrayColor];
            sel = @selector(clickCancelOrderAction);
        }   break;
        case RBProcessOrderActionCheckShopAddress: {
            color = [RBThemeConfig textGrayColor];
            bgColor = UIColor.whiteColor;
            text = @"查看商家地址";
            borderColor = [RBThemeConfig textGrayColor];
            sel = @selector(checkShopDetailBtnTapped);
        }   break;
        case RBProcessOrderActionCheckDetail: {
            color = UIColor.whiteColor;
            bgColor = [RBThemeConfig buttonBlueColor];
            text = @"查看详情";
            sel = @selector(checkDetailBtnTapped);
        }   break;
        case RBProcessOrderActionConfirmReceive: {
            color = UIColor.whiteColor;
            bgColor = [RBThemeConfig buttonBlueColor];
            text = @"确认收货";
            sel = @selector(confirmReceiveBtnTapped);
        }   break;
        case RBProcessOrderActionAppraise: {
            color = UIColor.whiteColor;
            bgColor = [RBThemeConfig buttonBlueColor];
            text = @"　评价　";
            sel = @selector(appraiseBtnTapped);
        }
            break;
    }
    UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:0 color:color align:0 text:[NSString stringWithFormat:@"　%@　", text] bgColor:bgColor radius:Zoom(15)];
    [view addPriority:1];
    [view rbAddTapGesWithTarget:self action:sel];
    if (borderColor)
    {
        [view rbSetBorderWitdh:1 borderColor:borderColor];
    }
    return view;
}

@end
